package com.zhangwei.day43bluetooth.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhangwei.day43bluetooth.R;

import java.util.List;

/**
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @Package com.zhangwei.day43bluetooth.adapter
 * @Description:
 * @date 15/10/27 上午10:40
 */

public class DeviceAdapter extends BaseAdapter {
    private Context context;
    private List<BluetoothDevice> list;

    private LayoutInflater inflater;


    public DeviceAdapter(Context context, List<BluetoothDevice> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_layout, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.nameTv.setText(list.get(position).getName());
        vh.addressTv.setText(list.get(position).getAddress());
        return convertView;
    }

    public static class ViewHolder {
        TextView nameTv;
        TextView addressTv;

        public ViewHolder(View convertView) {
            this.addressTv = (TextView) convertView.findViewById(R.id.item_address);
            this.nameTv = (TextView) convertView.findViewById(R.id.item_name);
        }
    }


}
